﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.IO;
using FisheryPlatform.Bussiness.LoginCookie;
using System.Configuration;

namespace FisheryPlatform.Bussiness.WebRuntime
{
    public class PassportModule : IHttpModule
    {
        public static List<string> VerifyPageExtensionList = new List<string>();
        public static List<string> SpecialPage = new List<string>();
        public void Dispose()
        {
        }

        public void Init(HttpApplication context)
        {
            HttpApplication Application = context;
            Application.BeginRequest += new EventHandler(Application_BeginRequest);
            Application.AuthorizeRequest += new EventHandler(Application_AuthorizeRequest);
            Application.EndRequest += new EventHandler(Application_EndRequest);

            VerifyPageExtensionList.Clear();
            VerifyPageExtensionList.Add(".aspx");
            VerifyPageExtensionList.Add(".ashx");
            VerifyPageExtensionList.Add(".html");
            VerifyPageExtensionList.Add(".htm");

            SpecialPage.Clear();
            SpecialPage.Add("upload.ashx");
            SpecialPage.Add("uploadtenderfile.ashx");
            SpecialPage.Add("validatecode.ashx");
        }

        void Application_EndRequest(object sender, EventArgs e)
        {
            HttpApplication app = sender as HttpApplication;
            if (VerifyPageExtensionList.Contains(Path.GetExtension(app.Request.Url.AbsolutePath).ToLower()))
            {
                app.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            }
        }

        void Application_BeginRequest(object sender, EventArgs e)
        {
            //var a = "";
        }

        void Application_AuthorizeRequest(object sender, EventArgs e)
        {
            #region
            HttpApplication app = sender as HttpApplication;
            if (SpecialPage.Contains(Path.GetFileName(app.Request.Url.AbsolutePath).ToLower()))
            {
                return;
            }
            if (VerifyPageExtensionList.Contains(Path.GetExtension(app.Request.Url.AbsolutePath).ToLower()))
            {
                if (app.User != null && app.User.Identity.IsAuthenticated)
                {
                    //LoginUserInfo userInfo;
                    //if (TradingPassport.Validate(userInfo.SmartID, userInfo.UserID))
                    //{
                    //    //app.Response.Write("OK<br/>");
                    //}
                    //else
                    //{
                    //    if (userInfo.UserID > 0)
                    //    {
                    //        LoginCookieManager.SignOut();
                    //        var url = SiteConfigManager.AppSettings["RepeatSignPrompt"];
                    //        url = string.Format(url, HttpUtility.UrlEncode(app.Request.Url.ToString()));
                    //        app.Response.Redirect(url, true);
                    //    }
                    //}
                }
                else
                {
                    if (Path.GetFileNameWithoutExtension(app.Request.Url.LocalPath).ToLower() != "login")
                    {
                        var returnUrl = HttpUtility.UrlEncode(app.Request.Url.ToString());
                        var loginUrl = string.Format(System.Web.Security.FormsAuthentication.LoginUrl, returnUrl);

                        app.Response.Redirect(loginUrl, true);
                    }
                }
            }
            #endregion
        }

    }
}
